package com.peanut.baby.widget.im;

import android.content.Context;
import android.widget.BaseAdapter;

import com.peanut.baby.InitManager;
import com.peanut.baby.R;

public class EaseChatRowImage extends EaseChatRowFile{

//    protected MsgThumbImageView imageView;
//    private EMImageMessageBody imgBody;

    public EaseChatRowImage(Context context, /*EMMessage message, */int position, BaseAdapter adapter) {
        super(context, position, adapter);
    }

    @Override
    protected void onInflateView() {
//        inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ? R.layout.ease_row_received_picture : R.layout.ease_row_sent_picture, this);
    }

    @Override
    protected void onFindViewById() {
//        percentageView = (TextView) findViewById(R.id.percentage);
//        imageView = (MsgThumbImageView) findViewById(R.id.image);
    }

    
    @Override
    protected void onSetUpView() {
//        imgBody = (EMImageMessageBody) message.getBody();
//        // received messages
//        if (message.direct() == EMMessage.Direct.RECEIVE) {
//            if (imgBody.thumbnailDownloadStatus() == EMFileMessageBody.EMDownloadStatus.DOWNLOADING ||
//                    imgBody.thumbnailDownloadStatus() == EMFileMessageBody.EMDownloadStatus.PENDING) {
//                imageView.loadAsResource(R.drawable.ease_default_image,maskBg());
//                setMessageReceiveCallback();
//            } else {
//                progressBar.setVisibility(View.GONE);
//                percentageView.setVisibility(View.GONE);
//                imageView.loadAsResource(R.drawable.ease_default_image,maskBg());
//                String thumbPath = imgBody.thumbnailLocalPath();
//
//                if (!new File(thumbPath).exists()) {
//                	// to make it compatible with thumbnail received in previous version
//                    thumbPath = EaseImageUtils.getThumbnailImagePath(imgBody.getLocalUrl());
//                }
//                showImageView(thumbPath, imageView, imgBody.getLocalUrl(), message);
//            }
//            return;
//        }
//
//        String filePath = imgBody.getLocalUrl();
//        String thumbPath = EaseImageUtils.getThumbnailImagePath(imgBody.getLocalUrl());
//        showImageView(thumbPath, imageView, filePath, message);
//        handleSendMessage();
    }
    
    @Override
    protected void onUpdateView() {
        super.onUpdateView();
    }
    
    @Override
    protected void onBubbleClick() {
//        Intent intent = new Intent(context, EaseShowBigImageActivity.class);
//        File file = new File(imgBody.getLocalUrl());
//        if (file.exists()) {
//            Uri uri = Uri.fromFile(file);
//            intent.putExtra("uri", uri);
//        } else {
//            // The local full size pic does not exist yet.
//            // ShowBigImage needs to download it from the server
//            // first
//            String msgId = message.getMsgId();
//            intent.putExtra("messageId", msgId);
//            intent.putExtra("localUrl", imgBody.getLocalUrl());
//        }
//        if (message != null && message.direct() == EMMessage.Direct.RECEIVE && !message.isAcked()
//                && message.getChatType() == ChatType.Chat) {
//            try {
//                EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//        context.startActivity(intent);
    }
    
    /**
     * load image into image view
     * 
//     * @param thumbernailPath
//     * @param iv
     * @return the image exists or not
     */
//    private boolean showImageView(final String thumbernailPath, final MsgThumbImageView iv, final String localFullSizePath, final EMMessage message) {
//        setImageSize(thumbernailPath);
//
//
//        Bitmap bitmap = EaseImageCache.getInstance().get(thumbernailPath);
//        if (bitmap != null) {
//            iv.loadAsPath(thumbernailPath,getImageMaxEdge(),getImageMaxEdge(),maskBg());
//            return true;
//        } else {
//            new AsyncTask<Object, Void, String>() {
//
//                @Override
//                protected String doInBackground(Object... args) {
//                    File file = new File(thumbernailPath);
//                    if (file.exists()) {
//                        return thumbernailPath;
//                    } else if (new File(imgBody.thumbnailLocalPath()).exists()) {
//                        return imgBody.thumbnailLocalPath();
//                    }
//                    else {
//                        if (message.direct() == EMMessage.Direct.SEND) {
//                            if (localFullSizePath != null && new File(localFullSizePath).exists()) {
//                                return localFullSizePath;
//                            } else {
//                                return null;
//                            }
//                        } else {
//                            return null;
//                        }
//                    }
//
//                }
//
//                protected void onPostExecute(String imagePath) {
//                    if (!Utils.StringUtils.isNullOrEmpty(imagePath)) {
//                        setImageSize(imagePath);
//
//                        if(imagePath.equals(localFullSizePath)){
//
//                            iv.loadAsPath(true,imagePath,message.getMsgId(),getImageMaxEdge(),getImageMaxEdge(),maskBg());
//                        }else{
//                            iv.loadAsPath(imagePath,getImageMaxEdge(),getImageMaxEdge(),maskBg());
//                        }
//
//                    } else {
//                        if (message.status() == EMMessage.Status.FAIL) {
//                            if (EaseCommonUtils.isNetWorkConnected(activity)) {
//                                new Thread(new Runnable() {
//
//                                    @Override
//                                    public void run() {
//                                        EMClient.getInstance().chatManager().downloadThumbnail(message);
//                                    }
//                                }).start();
//                            }
//                        }
//
//                    }
//
//                }
//            }.execute();
//
//            return true;
//        }
//
//    }

//    private void setImageSize(String thumbPath) {
//        int[] bounds = null;
//        if (thumbPath != null) {
//            bounds = BitmapDecoder.decodeBound(new File(thumbPath));
//        }
//        if (bounds == null) {
//            if (message.getType() == EMMessage.Type.IMAGE) {
//                EMImageMessageBody body = (EMImageMessageBody) message.getBody();
//                bounds = new int[]{body.getWidth(), body.getHeight()};
//            }
//
//        }
//
//        if (bounds != null) {
//            ImageUtil.ImageSize imageSize = ImageUtil.getThumbnailDisplaySize(bounds[0], bounds[1], getImageMaxEdge(), getImageMinEdge());
//            setLayoutParams(imageSize.width, imageSize.height, imageView);
//        }
//    }

    private int maskBg() {
        return R.drawable.ease_chat_mask_bg;
    }

    public static int getImageMaxEdge() {
        return (int) (165.0 / 320.0 * InitManager.getInstance().getScreenWidth());
    }

    public static int getImageMinEdge() {
        return (int) (76.0 / 320.0 * InitManager.getInstance().getScreenWidth());
    }


}
